<?php
/**
 * Created by PhpStorm.
 * User: lyh
 * Date: 2016/10/18
 * Time: 22:32
 */
namespace User\Controller;
use Think\Controller;

class RegisterController extends Controller{

    public function _initialize(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Methods:POST');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }
    /**
     *后台登录注册商铺信息界面
     */
    public function login(){
        if(IS_POST){
            $phoneNumber=I('post.phonenumber');
            $phoneCode=I('post.phonecode');
            $token=M('user')->where(array('phonenumber'=>$phoneNumber))->field('token')->find();           //获取token值
            $row = M('MsmCode')->where(['code_id'=>cookie(md5($phoneNumber))])->find();
            //测试账号--------------
            //测试账号--------------
            if($phoneNumber == '18883986242'){
                //登录成功  将用户token存入session中
                session('NOW_USER_TOKEN',$token['token']);
                $this->redirect('Admin/Index/index');//当前控制器下的这个方法
                exit;
            }
            //---------------------
            //---------------------
            if($row['code'] == $phoneCode && $phoneCode!==''){
                //登录成功  将用户token存入session中
                session('NOW_USER_TOKEN',$token['token']);
                $this->redirect('Admin/Index/index');//当前控制器下的这个方法
                exit;
            }else{
                echo '<script>alert("验证码错误，请1分钟后再试");location.href="'.U('login').'"</script>';
//                $this->redirect('login');
                return;
            }
        }else{
            $this->display();
        }
    }
    /**
     * 获取验证码
     */
    public function getPhoneCode($phonenumber){
        $apply=time();
        $user=M('user')->where(array('phonenumber'=>$phonenumber))->field('applytime')->select();
        if($user)//电话已保存在数据库中
        {
            $code = new \Message\Controller\IndexController();
//            session($phonenumber, mt_rand(1000, 9999), time() + 60);
            //----------------------
            //由于放到服务器上就不能使用session，所有这里的短信验证只能借用数据库的msmcode表来存放验证码
            //----------------------
            $phone_code = mt_rand(1000,9999);
            cookie(md5($phonenumber),md5($apply.mt_rand(1000,9999)));//验证码标识存入cookie中
            $code_data = [
                'code_id'=>cookie(md5($phonenumber)),
                'code'=>$phone_code,
            ];
            M('MsmCode')->add($code_data);
//            $code->sendDayuSmsPlus($phonenumber, (string)$phone_code);//发送短信

            //发送短信
            $Send = new \Org\Net\SendTemplateSMS();
            $Send->sendTemplateSMS($phonenumber,array($phone_code,'5'),'1');
            $timedata = array(
                'applytime' => date("Y-m-d H:i:s", $apply)
            );
            M('user')->where(array('phonenumber' => $phonenumber))->save($timedata);//更新登录时间
            $this->ajaxReturn(['status'=>'1']);
        }else{
            $this->ajaxReturn(['status'=>'0']);
        }
    }

    /**
     * 退出
     */
    public function logout(){
        session(null);
        $this->redirect('login');
    }




}